/**
 * 休眠账户激活分析器 - 对象中心化版本
 * 检测长期休眠后突然激活的账户
 */

class DormantAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '休眠账户激活';
        this.description = '检测长期休眠后突然激活的异常账户';
    }

    async analyze(params = {}) {
        const dormantMonths = params.dormantMonths || 6;
        const minAmount = params.minAmount || 50000;

        const accountFilter = this.getBankAccountFilter();

        const sql = `
            WITH OrderedTrans AS (
                SELECT 
                    account_card,
                    customer_name,
                    transaction_time,
                    transaction_amount,
                    LAG(transaction_time) OVER (PARTITION BY account_card ORDER BY transaction_time) as prev_time
                FROM bank_transactions
                WHERE case_id = ?
                    ${accountFilter}
            )
            SELECT 
                account_card,
                customer_name,
                transaction_time as wake_time,
                transaction_amount,
                prev_time as last_active_time,
                ROUND((julianday(transaction_time) - julianday(prev_time)) / 30.0, 1) as dormant_months
            FROM OrderedTrans
            WHERE prev_time IS NOT NULL
                AND (julianday(transaction_time) - julianday(prev_time)) >= (? * 30)
                AND ABS(transaction_amount) >= ?
            ORDER BY dormant_months DESC
            LIMIT 50
        `;

        const result = await this.runQuery(sql, [
            this.caseId,
            dormantMonths,
            minAmount
        ]);

        if (!result.success) {
            return this.createResult('dormant', this.name, [], '查询失败: ' + result.message, 'danger');
        }

        const data = result.data;
        const summary = data.length > 0
            ? `检测到 ${data.length} 个休眠账户激活`
            : '未检测到休眠账户激活';

        const level = data.length > 3 ? 'warning' : (data.length > 0 ? 'info' : 'info');

        return this.createResult('dormant', this.name, data, summary, level);
    }

    renderResult(data) {
        if (!data || data.length === 0) {
            return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 未发现休眠账户激活</div>';
        }

        let html = `
      <div class="result-summary" style="padding:15px; background:#fff3cd; border-left:4px solid #ffc107; margin-bottom:15px;">
        <strong>💤 发现 ${data.length} 个休眠账户激活</strong>
        <p style="margin:5px 0 0; font-size:0.9rem;">长期休眠账户突然激活，可能用于特殊目的</p>
      </div>
      
      <table class="analysis-result-table" style="width:100%; border-collapse:collapse; font-size:0.9rem;">
        <thead style="background:#f5f7fa;">
          <tr>
            <th style="padding:10px; border:1px solid #e0e0e0;">账号/户名</th>
            <th style="padding:10px; border:1px solid #e0e0e0;">最后活跃时间</th>
            <th style="padding:10px; border:1px solid #e0e0e0;">激活时间</th>
            <th style="padding:10px; border:1px solid #e0e0e0; text-align:center; background:#fff3cd;">休眠(月)</th>
            <th style="padding:10px; border:1px solid #e0e0e0; text-align:right;">激活金额</th>
          </tr>
        </thead>
        <tbody>
    `;

        data.forEach((row, index) => {
            const rowStyle = index % 2 === 0 ? 'background:#fafafa;' : '';
            html += `<tr style="${rowStyle}">`;
            html += `<td style="padding:8px; border:1px solid #e0e0e0;">${row.customer_name}<br><small style="color:#909399;">${row.account_card}</small></td>`;
            html += `<td style="padding:8px; border:1px solid #e0e0e0;">${this.formatDate(row.last_active_time)}</td>`;
            html += `<td style="padding:8px; border:1px solid #e0e0e0;">${this.formatDate(row.wake_time)}</td>`;
            html += `<td style="padding:8px; border:1px solid #e0e0e0; text-align:center; font-weight:bold; color:#e6a23c;">${row.dormant_months}</td>`;
            html += `<td style="padding:8px; border:1px solid #e0e0e0; text-align:right; color:#f56c6c; font-weight:bold;">¥${this.formatAmount(row.transaction_amount)}</td>`;
            html += '</tr>';
        });

        html += '</tbody></table>';
        return html;
    }
}

window.DormantAnalyzer = DormantAnalyzer;
